import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import train_test_split

# 加载数据集
breast_cancer = load_breast_cancer()
features, labels = breast_cancer.data, breast_cancer.target
# 划分训练集和测试集
features_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size=0.2,
                                                                            random_state=42)
# 定义决策树和 AdaBoost 分类器
decision_tree_classifier = DecisionTreeClassifier()
ada_boost_classifier = AdaBoostClassifier(DecisionTreeClassifier(), algorithm='SAMME', n_estimators=30,
                                          learning_rate=0.1)
decision_tree_classifier.fit(features_train, labels_train)
ada_boost_classifier.fit(features_train, labels_train)
score_decision_tree = decision_tree_classifier.score(features_test, labels_test)
score_ada_boost = ada_boost_classifier.score(features_test, labels_test)
print("---预测准确率---")
print("决策树: ", score_decision_tree)
print("Ada Boost: ", score_ada_boost)

# 观察弱分类器数量对分类准确度的影响
ada_boost_scores = []
for i in range(1, 50):
    ada_boost_classifier.estimators_ = i
    ada_boost_classifier.fit(features_train, labels_train)
    ada_boost_score = ada_boost_classifier.score(features_test, labels_test)
    ada_boost_scores.append(ada_boost_score)

plt.figure()
plt.title('AdaBoost')
plt.xlabel('n_estimators')
plt.ylabel('Accuracy')
plt.plot(range(1, 50), ada_boost_scores)
plt.show()
